#!/usr/bin/env node
var fs          = require('fs');
var pathOrig    = require('path');

/*
input   : array of input files
output  : array of output files

@return :
true    : if any of the inputs is younger than any of the outputs
false   : all outputs are younger than inputs
*/
function outputStale(params) {

    if (!(params.input instanceof Array)) {
        params.input = [params.input];
    }

    if (!(params.output instanceof Array)) {
        params.output = [params.output];
    }

    if (params.diagnose) {
        console.log("outputStale("+JSON.stringify(params, undefined, 2)+")")
    }


    for (var i = 0; i < params.input.length; i++) {
        var input = params.input[i];
        if (!fs.existsSync(input)) {
            if (params.diagnose) {
                console.log("outputStale: input '"+input+"' not found and ignored");
            }
            continue;
        }
        var inputMDate = new Date(fs.statSync(input).mtime);
        for (var j = 0; j < params.output.length; j++) {
            var output = params.output[j];
            if (!fs.existsSync(output)) {
                if (params.diagnose) {
                    console.log("outputStale: returning true, because '"+output+"' doesn't exist.");
                }
                return true;
            }
            var outputMDate = new Date(fs.statSync(output).mtime);
            if (params.diagnose) {
                console.log("outputStale: comparing output:'"+output+"' with input:'"+input+"'");
                console.log("input  :"+inputMDate.toString());
                console.log("output :"+outputMDate.toString());
            }
            if (outputMDate.getTime() < inputMDate.getTime()) {
                if (params.diagnose) {
                    console.log("outputStale: returning true, because output is older than input.");
                }
                return true;
            }
        }
    }
    if (params.diagnose) {
        console.log("outputStale: returning false");
    }
    return false;
}

function rmdirSyncForce(path) {
    var files, file, fileStats, i, filesLength;
    if (path[path.length - 1] !== '/') {
        path = path + '/';
    }

    files = fs.readdirSync(path);
    filesLength = files.length;

    if (filesLength) {
        for (i = 0; i < filesLength; i += 1) {
            file = files[i];

            fileStats = fs.statSync(path + file);
            if (fileStats.isFile()) {
                fs.unlinkSync(path + file);
            }
            if (fileStats.isDirectory()) {
                rmdirSyncForce(path + file);
            }
        }
    }
    fs.rmdirSync(path);
}

function copyFolder(src, dst) {
    var files, file, fileStats, i, filesLength;
    if (src[src.length - 1] !== '/') {
        src = src + '/';
    }
    if (dst[dst.length - 1] !== '/') {
        dst = dst + '/';
    }

    if (fs.existsSync(dst)) {
        rmdirSyncForce(dst);
    }

    files = fs.readdirSync(src);
    filesLength = files.length;

    mkdirRecursive(dst);
    if (filesLength) {
        for (i = 0; i < filesLength; i += 1) {
            var file = files[i];

            var fileStats = fs.statSync(src + file);
            if (fileStats.isFile()) {
                fs.writeFileSync(dst + file, fs.readFileSync(src + file));
            }
            if (fileStats.isDirectory()) {
                copyFolder(src + file, dst + file);
            }
        }
    }
}

function mkdirRecursive(path) {
    var dir = pathOrig.dirname(path);
    if (dir != path) {
        mkdirRecursive(dir);
    }
    if (!fs.existsSync(path)) {
        fs.mkdirSync(path);
    }
}


module.exports = {
    rmdirSyncForce  : rmdirSyncForce,
    outputStale     : outputStale,
    copyFolder      : copyFolder,
    mkdirRecursive  : mkdirRecursive
}


